﻿using Furion.LinqBuilder;
using OAS.Application.System.Dtos;
using OAS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OAS.Application.MeetingApp.Services
{
    public class MeetingService : Repository<Meeting>, IMeetingService, ITransient
    {
        public async Task<List<Meeting>> GetAllMeeting()
        {
            return await base.GetListAsync();
        }
        public async Task<object> GetMeetingList(DtoQuery dto)
        {
            dto.Time = dto.Time == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)dto.Time, TimeZoneInfo.Local);
            Expression<Func<Meeting, bool>> sqlWhere = Expressionable.Create<Meeting>()
                    .AndIF(!string.IsNullOrEmpty(dto.KeyWord), o => o.MeetingTitle.Contains(dto.KeyWord) || o.MeetingRecord.Contains(dto.KeyWord))
                    .AndIF(!string.IsNullOrEmpty(dto.Department), o => o.DepartmentId.Equals(dto.Department))
                    .AndIF(dto.Time != null, o => o.MeetingDate.Value.Date.Equals(dto.Time.Value.Date))
                    .AndIF(UserContext.CurrentUser.RoleName != "总经理" && !UserContext.IsAdmin, o => o.DepartmentId == UserContext.CurrentUser.Department_Id)
                    .ToExpression();
            RefAsync<int> total = 0;
            var list = await base.AsQueryable().Where(sqlWhere).ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            return new { list, total };
        }
        public async Task AddMeeting(Meeting meeting)
        {
            meeting.MeetingDate = meeting.MeetingDate == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)meeting.MeetingDate, TimeZoneInfo.Local);
            await base.InsertAsync(meeting);
        }
        public async Task UpdateMeeting(Meeting meeting)
        {
            meeting.MeetingDate = meeting.MeetingDate == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)meeting.MeetingDate, TimeZoneInfo.Local);
            await base.UpdateAsync(meeting);
        }
        public async Task DeleteMeeting(string[] ids)
        {
            await base.DeleteByIdsAsync(ids);
        }
    }
}
